<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col :span="22"><div class="grid-content bg-purple"><span class="size">说明: 目前支持学科和关键字条件筛选</span></div></el-col>
        <el-col :span="2">
          <el-button type="success" size="mini" @click="$router.push('/questions/new')" icon="el-icon-edit" >新增试题</el-button>
        </el-col>
      </el-row>

      <el-form ref="form" :model="formInline" label-width="80px">
        <!-- 第一排 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="科学">
              <el-select style="display:block" v-model="formInline.subjectID" @change="changetwotags(formInline.subjectID)" placeholder="请选择">
                <el-option v-for="item in form" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select style="display:block" v-model="formInline.twoLeveIDirectory" placeholder="请选择">
                <el-option v-for="item in twolist" :key="item.id" :label="item.directoryName" :value="item.directoryName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select style="display:block" v-model="formInline.tags" placeholder="请选择">
                <el-option v-for="item in tagslist" :key="item.id" :label="item.tagName" :value="item.tagName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" >
              <el-input v-model="formInline.keyword" placeholder="根据题干搜索" />
            </el-form-item>
          </el-col>
        </el-row>

       <!-- 第二排 -->
       <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select style="display:block" v-model="formInline.questionTyoe" placeholder="请选择">
                <el-option v-for="item in questionType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select style="display:block" v-model="formInline.difficulty" placeholder="请选择">
                <el-option v-for="item in difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select style="display:block" v-model="formInline.direction" placeholder="请选择">
                <el-option v-for="(item,index) in direction" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select style="display:block" v-model="formInline.creatorID" placeholder="请选择">
                <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
       </el-row>

       <!-- 第三排 -->
       <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="formInline.remarks" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="formInline.shortName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-col :span="12" style="padding-right: 5px">
                <el-select  v-model="formInline.province" @change="choseProvince(formInline.province)"  placeholder="请选择">
                  <el-option v-for="(item,index) in provinces" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-col>
              <el-col :span="12">
                <el-select v-model="formInline.city" placeholder="请选择">
                  <el-option v-for="(item,index) in citylist" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="btn">
            <el-row >
              <el-button  @click="removeform">清除</el-button>
              <el-button  type="primary">搜索</el-button>
            </el-row>
          </el-col>
       </el-row>
      </el-form>
      <template>
      <el-tabs type="card">
        <el-tab-pane name="first"><div slot="label" @click="allchkState()">全部</div></el-tab-pane>
        <el-tab-pane name="second"><div slot="label" @click="checkPending()">待审核</div></el-tab-pane>
        <el-tab-pane name="third"><div slot="label" @click="checked()">已审核</div></el-tab-pane>
        <el-tab-pane name="fourth"><div slot="label" @click="denied()">已拒绝</div></el-tab-pane>
      </el-tabs>
    </template>

      <!-- 分割 -->
      <el-alert class="alert" show-icon type="info" :title="`数据一共有：${counts}条`" :closable="false" />
      <!-- 列表数据 -->
      <el-table
        :header-cell-style="{background:'#fafafa','border-bottom':'2px solid #e8e8e8'}"
        :data="listss"
        style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="250" />
        <el-table-column prop="subject" label="学科" width="120" />
        <el-table-column prop="catalog" label="目录" width="120" />
        <el-table-column prop="questionType" label="题型" :formatter="formatterquestionType" width="120" />
        <el-table-column label="题干" prop="question" width="350">
          <template #default={row}>
            <span v-html="row.question"></span>
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="200">
          <template #default={row}>
            {{ formatDate(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" :formatter="formatterdifficulty" label="难度" width="120" />
        <el-table-column prop="creator" label="录入人" width="120" />
        <el-table-column prop="chkState" label="审核状态" :formatter="formatterchkState" width="120" />
        <el-table-column prop="chkRemarks" label="审核意见" width="120" />
        <el-table-column prop="chkUser" label="审核人" width="120" />
        <el-table-column prop="publishState" label="发布状态" :formatter="formatterpublishState" width="120" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default='{row}'>
            <el-button type="text" @click="clickdialog(row)">预览</el-button>
            <el-button type="text" @click="chkStatedialog(row)" :disabled="row.chkState != 0">审核</el-button>
            <el-button type="text" @click="routerPush(row.id)" :disabled="row.publishState != 0">修改</el-button>
            <el-button type="text" @click="clickchoicee(row)">{{row.publishState==0 ? '上架':'下架'}}</el-button>
            <el-button type="text" @click="clickdel(row)" :disabled="row.publishState != 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <div class="block">
        <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10]"
        :page-size="data.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="counts">
        </el-pagination>
      </div>
       <!-- 预览弹层 -->
      <el-dialog v-if="showDialog" title="题目预览" :visible.sync="showDialog" width="50%">
        <Topicpreview
          :timudata="timudata"
          @close="showDialog = false"
          :questionType="questionType"
          :difficulty="difficulty"
        />
      </el-dialog>
      <!-- 审核弹层 -->
      <el-dialog title="题目审核" :visible="dialogchkState" width="25%" @close="handlCloserow">
        <el-radio v-model="rowchkState.chkState" :label="1">通过</el-radio>
        <el-radio v-model="rowchkState.chkState" :label="2">拒绝</el-radio>
        <el-input type="textarea" placeholder="请输入审核意见" v-model="rowchkState.chkRemarks" style="width: 100%;padding-top: 20px;padding-bottom: 50px" />
        <div class="btn">
          <el-button @click="handlCloserow">取消</el-button>
          <el-button type="primary" @click="chkStaterow">确认</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { subList } from '@/api/hmmm/subjects.js' // 获取学科列表
import { list as mulu } from '@/api/hmmm/directorys.js' // 目录
import { list as tags } from '@/api/hmmm/tags.js' // 标签列表
import { difficulty, questionType, direction, chkType, publishType } from '@/api/hmmm/constants.js' // difficulty题目难度 questionType 题型 direction方向
import { list as lists } from '@/api/base/users.js' // 录入人列表
import { provinces, citys } from '@/api/hmmm/citys.js' // 获取城市
import { remove, choice, detail, choiceCheck, choicePublish } from '@/api/hmmm/questions.js' // 表单数据
import Topicpreview from '@/module-hmmm/components/questions-preview' // 导入弹层
export default {
  components: {
    Topicpreview
  },
  data () {
    return {
      formInline: { // 学科城市联动
        twoLeveIDirectory: '',
        tags: '',
        city: ''
      },
      form: [], // 学科列表
      twolist: [], // 二级目录
      tagslist: [], // 标签
      questionType: [], // 题型
      difficulty: [], // 题目难度
      direction: [], // 方向
      list: [], // 录入人
      provinces: [], // 所有城市
      citylist: [], // 区县

      listss: [], // 精选题库列表
      counts: null, // 总条数
      chkState: [], // 审核
      publishState: [], // 发布
      data: { // 全部基础题库列表数据
        page: 1, // 当前页码
        pagesize: 5 // 页显示数据的条数
      },
      currentPage: 1, // 当前页面
      showDialog: false, // 弹层开关
      timudata: {}, // 题目预览数据
      dialogchkState: false, // 题目审核弹框
      rowchkState: {} // 审批的数据
    }
  },
  created () {
    this.getquestions()
    this.Arraydirection()
    this.ArrayquestionType()
    this.Arraydifficulty()
    this.getlists()
    this.getprovinces()
    this.getlistss()
  },
  methods: {
    // 获取学科
    async getquestions () {
      const res = await subList()
      // console.log(res)
      this.form = res.data.items
    },
    // 点击学科获取二级目录 标签
    async changetwotags (e) {
      this.formInline.twoLeveIDirectory = ''
      this.formInline.tags = ''
      const { data: { items } } = await mulu({ subjectID: e })
      // console.log(items)
      this.twolist = items
      const { data } = await tags()
      // console.log(data.items)
      this.tagslist = data.items
    },
    // 试题
    ArrayquestionType () {
      this.questionType = questionType
      // console.log(this.questionType)
    },
    // 题型
    Arraydifficulty () {
      this.difficulty = difficulty
    },
    // 试题类型枚举
    formatterquestionType (row, column, cellValue, index) {
      const obj = questionType.find(item => item.value === +cellValue)
      return obj.label
    },
    // 难度的枚举
    formatterdifficulty (row, column, cellValue, index) {
      const obj = difficulty.find(item => item.value === +cellValue)
      return obj.label
    },
    // 方向
    Arraydirection () {
      this.direction = direction
      // console.log(this.direction)
    },
    // 录入人
    async getlists () {
      const { data: { list } } = await lists()
      this.list = list
    },
    // 获取城市
    async getprovinces () {
      const res = await provinces()
      this.provinces = res
      // console.log(this.provinces)
    },
    // 市区联动
    choseProvince (e) {
      this.formInline.city = ''
      this.citylist = citys(e)
    },
    // 清空表单
    removeform () {
      this.formInline = {}
    },
    // //
    // //
    // 获取精选题库列表
    async getlistss () {
      const { data } = await choice(this.data)
      // console.log(data)
      this.counts = data.counts // 总条数
      this.listss = data.items // 列表数据
      // console.log(this.listss)
    },
    // tab全部
    async allchkState () {
      const { data } = await choice(this.data)
      // console.log(data)
      this.counts = data.counts // 总条数
      this.listss = data.items // 列表数据
    },
    // 待审核
    async checkPending () {
      const { data: { items } } = await choice(this.data)
      const array = items.filter(item => item.chkState === 0)
      this.counts = array.length
      this.listss = array
    },
    // 已审核
    async checked () {
      const { data: { items } } = await choice(this.data)
      const array = items.filter(item => item.chkState === 1)
      this.counts = array.length
      this.listss = array
    },
    // 已拒绝
    async denied () {
      const { data: { items } } = await choice(this.data)
      const array = items.filter(item => item.chkState === 2)
      this.counts = array.length
      this.listss = array
    },
    // 时间转换
    formatDate (date) {
      var dayjs = require('dayjs')
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    handleSizeChange (val) { // 调整页面显示数据的条数
      // console.log(val)
      this.data.pagesize = val
      this.getlistss()
    },
    handleCurrentChange (val) { // 当前在第几页
      // console.log(val)
      this.data.page = val
      this.getlistss()
    },

    // 数据父传子
    async clickdialog (value) {
      const { data } = await detail(value)
      this.timudata = data
      this.showDialog = true
    },
    // 审核枚举
    formatterchkState (row, column, cellValue, index) {
      const obj = chkType.find(item => item.value === (+cellValue + 1))
      return obj.label
    },
    // 发布枚举
    formatterpublishState (row, column, cellValue, index) {
      const obj = publishType.find(item => item.value === (+cellValue + 1))
      return obj.label
    },
    // 弹出审批框并赋值变量
    chkStatedialog (obj) {
      this.dialogchkState = true
      this.rowchkState = obj
    },
    // 审批框关闭
    handlCloserow () {
      this.dialogchkState = false
    },
    // 点击确定审批完成
    async chkStaterow () {
      if (this.rowchkState.chkRemarks === null) {
        return this.$message.warning('请输入审核意见!')
      } else {
        await choiceCheck({ id: this.rowchkState.id, ...this.rowchkState })
        this.dialogchkState = false
        this.getlistss()
      }
    },
    // 点击修改跳转试题录入
    routerPush (id) {
      this.$router.push({
        path: '/questions/new',
        query: { id }
      })
    },
    // 上架下架
    clickchoicee (row) {
      let publishState = row.publishState
      if (publishState === 1) {
        publishState = 0
      } else {
        publishState = 1
      }
      this.$confirm(`您确认${publishState === 1 ? '上架' : '下架'}这道题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        const res = await choicePublish({ id: row.id, publishState })
        // console.log(res)
        if (res.status !== 200) return this.$message.error('上架失败!')
        this.$message.success(`${publishState === 1 ? '上架' : '下架'}成功!`)
        this.getlistss()
      }).catch(e => e)
    },
    // 删除
    async clickdel (data) {
      console.log(data)
      const res = await this.$confirm('确定要删除吗?').catch(error => error)
      if (res !== 'confirm') return
      await remove(data)
      this.$message.success('删除成功')
      if (this.listss.length === 1 && this.data.page > 1) this.data.page--
      this.getlistss()
    }
  }
}
</script>

<style scoped>
.container {
  padding: 15px;
}
.alert {
  margin: 10px  0;
}
.size {
  font-size: 12px;
  color: pink;
}
.block {
  display: flex;
  justify-content: end;
  margin-top: 15px;
}
.el-button--mini {
  margin-bottom: 10px;
}
.btn {
  display: flex;
    justify-content: end;
}

</style>
